home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / src / GLperf3.12-src.lha / GLperf / Vertex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-01  |  25.3 KB  |  789 lines

  1. /*
  2. //   (C) COPYRIGHT International Business Machines Corp. 1993
  3. //   All Rights Reserved
  4. //   Licensed Materials - Property of IBM
  5. //   US Government Users Restricted Rights - Use, duplication or
  6. //   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. //
  8.  
  9. //
  10. // Permission to use, copy, modify, and distribute this software and its
  11. // documentation for any purpose and without fee is hereby granted, provided
  12. // that the above copyright notice appear in all copies and that both that
  13. // copyright notice and this permission notice appear in supporting
  14. // documentation, and that the name of I.B.M. not be used in advertising
  15. // or publicity pertaining to distribution of the software without specific,
  16. // written prior permission. I.B.M. makes no representations about the
  17. // suitability of this software for any purpose.  It is provided "as is"
  18. // without express or implied warranty.
  19. //
  20. // I.B.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  21. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL I.B.M.
  22. // BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  23. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  24. // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  25. // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26. //
  27. // Authors:  Barry Minor, John Spitzer, IBM AWS Graphics Systems (Austin)
  28. //
  29. */
  30.  
  31. #include <math.h>
  32. #include "Vertex.h"
  33. #include "VertexX.h"
  34. #include "VertexAX.h"
  35. #include "VertA11X.h"
  36. #include <malloc.h>
  37.  
  38. void new_Vertex(VertexPtr this)
  39. {
  40.     new_Primitive((PrimitivePtr)this);
  41.  
  42.     this->layoutSides = 4;
  43.     this->layoutLeft = -1.;
  44.     this->layoutRight = 1.;
  45.     this->layoutTop = 1.;
  46.     this->layoutBottom = -1.;
  47.     this->traversalData = 0;
  48.     /* Set virtual functions */
  49.     this->SetState = Vertex__SetState;
  50.     this->delete = delete_Vertex;
  51.     this->Initialize = Vertex__Initialize;
  52.     this->Cleanup = Vertex__Cleanup;
  53.     this->SetExecuteFunc = Vertex__SetExecuteFunc;
  54.     this->PixelSize = Vertex__Size;
  55. }
  56.  
  57. void delete_Vertex(TestPtr thisTest)
  58. {
  59.     VertexPtr this = (VertexPtr)thisTest;
  60.  
  61.     delete_Primitive(thisTest);
  62. }
  63.  
  64. void Vertex__Initialize(TestPtr thisTest)
  65. {
  66.     VertexPtr this = (VertexPtr)thisTest;
  67.     this->numBgnEnds = (int)ceil((GLfloat)this->numObjects/(GLfloat)this->objsPerBgnEnd);
  68.     this->numObjects = this->numBgnEnds * this->objsPerBgnEnd;
  69.     this->Layout(this);
  70.     Vertex__AddTraversalData(this);
  71. }
  72.  
  73. void Vertex__Cleanup(TestPtr thisTest)
  74. {
  75.     VertexPtr this = (VertexPtr)thisTest;
  76.  
  77.     if (this->traversalData) AlignFree(this->traversalData);
  78. }
  79.  
  80. static void InitializeJumpTable( void)
  81. {
  82.     VertexExecuteTableTable[0] = VertexExecuteTable00;
  83.     VertexExecuteTableTable[1] = VertexExecuteTable01;
  84.     VertexExecuteTableTable[2] = VertexExecuteTable02;
  85.     VertexExecuteTableTable[3] = VertexExecuteTable03;
  86.     VertexExecuteTableTable[4] = VertexExecuteTable04;
  87.     VertexExecuteTableTable[5] = VertexExecuteTable05;
  88.     VertexExecuteTableTable[8] = VertexExecuteTable08;
  89.     VertexExecuteTableTable[9] = VertexExecuteTable09;
  90.     VertexExecuteTableTable[10] = VertexExecuteTable10;
  91.     VertexExecuteTableTable[11] = VertexExecuteTable11;
  92.     VertexExecuteTableTable[12] = VertexExecuteTable12;
  93.     VertexExecuteTableTable[13] = VertexExecuteTable13;
  94.     VertexExecuteTableTable[16] = VertexExecuteTable16;
  95.     VertexExecuteTableTable[17] = VertexExecuteTable17;
  96.     VertexExecuteTableTable[18] = VertexExecuteTable18;
  97.     VertexExecuteTableTable[19] = VertexExecuteTable19;
  98.     VertexExecuteTableTable[20] = VertexExecuteTable20;
  99.     VertexExecuteTableTable[21] = VertexExecuteTable21;
  100. }
  101.  
  102. void Vertex__SetExecuteFunc(TestPtr thisTest)
  103. {
  104.     VertexPtr this = (VertexPtr)thisTest;
  105.     VertexFunc function;
  106.     VertexFile file;
  107.     VertexArrayFunc arrayfunction;
  108.     ExecuteFunc* VertexExecuteTable;
  109.  
  110. #ifdef GL_EXT_vertex_array
  111.     if (this->vertexArray) {
  112.     arrayfunction.word = 0;
  113.     arrayfunction.bits.colorData = (this->environ.bufConfig.rgba) ?
  114.                        (this->colorData == PER_VERTEX) : 0;
  115.     arrayfunction.bits.indexData = (this->environ.bufConfig.rgba) ?
  116.                        0 : (this->colorData == PER_VERTEX);
  117.     arrayfunction.bits.normalData = (this->normalData == PER_VERTEX);
  118.     arrayfunction.bits.textureData = (this->environ.bufConfig.rgba) ? 
  119.                                        (this->textureData == PER_VERTEX) : 0;
  120.     arrayfunction.bits.functionPtrs = this->loopFuncPtrs;
  121.        
  122.     this->Execute = VertexArrayExecuteTable[arrayfunction.word];
  123.     return;
  124.     }
  125. #endif
  126.  
  127. #ifdef GL_VERSION_1_1
  128.     if (this->vertexArray11) {
  129.     VertexArray11Func arrayfunction;
  130.  
  131.     arrayfunction.word = 0;
  132.     arrayfunction.bits.colorData = (this->environ.bufConfig.rgba) ?
  133.                        (this->colorData == PER_VERTEX) : 0;
  134.     arrayfunction.bits.indexData = (this->environ.bufConfig.rgba) ?
  135.                        0 : (this->colorData == PER_VERTEX);
  136.     arrayfunction.bits.normalData = (this->normalData == PER_VERTEX);
  137.     arrayfunction.bits.textureData = (this->environ.bufConfig.rgba) ? 
  138.                                        (this->textureData == PER_VERTEX) : 0;
  139.     arrayfunction.bits.functionPtrs = this->loopFuncPtrs;
  140.     arrayfunction.bits.drawElements = this->drawElements;
  141. #ifdef GL_SGI_compiled_vertex_array
  142.     arrayfunction.bits.lockArrays = this->lockArrays;
  143. #endif
  144.     arrayfunction.bits.interleaved = this->interleavedData;
  145.        
  146.     this->Execute = VertexArray11ExecuteTable[arrayfunction.word];
  147.     return;
  148.     }
  149. #endif
  150.  
  151.     InitializeJumpTable();
  152.  
  153.     file.word = 0;
  154.     function.word = 0;
  155.  
  156.     function.bits.functionPtrs = this->loopFuncPtrs;
  157.     if (this->environ.bufConfig.rgba)
  158.         file.bits.visual        = RGB;
  159.     else
  160.         file.bits.visual        = CI;
  161.     switch(this->colorData) {
  162.         case None:
  163.             file.bits.colorData     = NONE;
  164.             break;
  165.         case PerFacet:
  166.             file.bits.colorData     = PER_FACET;
  167.             break;
  168.         case PerVertex:
  169.             file.bits.colorData     = PER_VERTEX;
  170.             break;
  171.     }
  172.     switch(this->normalData) {
  173.         case None:
  174.             file.bits.normalData    = NONE;
  175.             break;
  176.         case PerFacet:
  177.             file.bits.normalData    = PER_FACET;
  178.             break;
  179.         case PerVertex:
  180.             file.bits.normalData    = PER_VERTEX;
  181.             break;
  182.     }
  183.     if (this->environ.bufConfig.rgba) {
  184.         switch(this->textureData) {
  185.             case None:
  186.                 function.bits.textureData   = NONE;
  187.                 break;
  188.             case PerVertex:
  189.                 function.bits.textureData   = PER_VERTEX;
  190.                 break;
  191.         }
  192.     }
  193.  
  194.     if (this->vertsPerFacet > 8) {
  195.     /* Special case, only used for many-sided polygons */
  196.     function.bits.vertsPerFacet = 9 - 1;
  197.     function.bits.unrollAmount  = 1 - 1;
  198.     } else {
  199.     function.bits.vertsPerFacet = this->vertsPerFacet - 1;
  200.     function.bits.unrollAmount  = this->loopUnroll - 1;
  201.     }
  202.  
  203.     /* Dimensions of data to be traversed */
  204.     if (this->environ.bufConfig.rgba) {
  205.     switch (this->texture) {
  206.         case GL_TEXTURE_1D: 
  207.         function.bits.textureDim = 0; break;
  208.         case Off:
  209.         /* This may seem bizarre, but this path always exists */
  210.         case GL_TEXTURE_2D: 
  211.         function.bits.textureDim = 1; break;
  212. #ifdef GL_EXT_texture3D
  213.         case GL_TEXTURE_3D_EXT: 
  214.         function.bits.textureDim = 2; break;
  215. #endif
  216. #ifdef GL_SGIS_texture4D
  217.         case GL_TEXTURE_4D_SGIS: 
  218.         function.bits.textureDim = 3; break;
  219. #endif
  220.     }
  221.         function.bits.colorDim = this->colorDim - 3;
  222.     }
  223.     function.bits.vertexDim = this->vertexDim - 2;
  224.  
  225.     /* This looks nasty with two levels of function pointer
  226.      * indirection (and it is!)  This nastiness is necessitated
  227.      * by some compilers' inabilities to compile files with very
  228.      * large (i.e. >64K entries) arrays.  We have split this
  229.      * large jump table into 18 arrays.  Each array has an
  230.      * entry in the "TableTable" which is located in VertexX.h
  231.      * The jump tables themselves are located in Vert00F.h,
  232.      * Vert01F.h, Vert02F.h, ..., Vert21F.h
  233.      */
  234.     VertexExecuteTable = VertexExecuteTableTable[file.word];
  235.     this->Execute = VertexExecuteTable[function.word];
  236. }
  237.  
  238. float Vertex__Size(TestPtr thisTest)
  239. {
  240.     VertexPtr this = (VertexPtr)thisTest;
  241.     return this->size;
  242. }
  243.  
  244. int Vertex__SetState(TestPtr thisTest)
  245. {
  246.     VertexPtr this = (VertexPtr)thisTest;
  247.     GLfloat light[4];
  248.     GLfloat material[4];
  249.     int currentLight;
  250.     GLfloat theta;
  251.     int i;
  252.     int numLights = 0;
  253.     const GLfloat pi = 3.141592654;
  254.     const GLfloat two_pi = 2.0 * pi;
  255.  
  256.     /* set parent state */
  257.     if (Primitive__SetState(thisTest) == -1) return -1;
  258.     Primitive__SetProjection((PrimitivePtr)this, this->vertexDim);
  259.  
  260.     /* set own state */
  261.     if(min(this->environ.windowWidth, this->environ.windowHeight)==0)
  262.       this->layoutPadding = 1.0;
  263.     else
  264.       this->layoutPadding = 1./min(this->environ.windowWidth, this->environ.windowHeight);
  265.  
  266. #ifdef GL_EXT_vertex_array
  267.     if (this->vertexArray && (this->colorData == PerFacet || this->normalData == PerFacet))
  268.     return -1;
  269. #endif
  270.  
  271. #ifdef GL_VERSION_1_1
  272.     if (this->vertexArray11 && (this->colorData == PerFacet || this->normalData == PerFacet))
  273.     return -1;
  274. #endif
  275.  
  276.     if (this->loopUnroll % this->vertsPerFacet != 0 &&
  277.         this->vertsPerFacet % this->loopUnroll != 0)
  278.         return -1;
  279.  
  280.     /* set lighting stuff */
  281.     if (this->numLocalLights==0 && this->numInfiniteLights==0) { /* Lighting turned off */
  282.     glDisable(GL_LIGHTING);
  283.     if (this->environ.bufConfig.rgba) {
  284.         glColor3f(1.0,1.0,1.0);
  285.     } else {
  286.         glIndexi((1 << this->environ.bufConfig.indexSize) - 1);
  287.     }
  288.     } else { /* Lighting on */
  289.         numLights = this->numInfiniteLights + this->numLocalLights;
  290.         if (numLights > 8) 
  291.             return -1;
  292.     /* Compute light colors and positions */
  293.     theta = 0.0;
  294.         /* First, define the infinite lights */
  295.         for (currentLight=0; currentLight<this->numInfiniteLights; currentLight++) {
  296.         CalcRGBColor(theta/two_pi, &light[0], &light[1], &light[2]);
  297.         light[3] = 1.0;
  298.         glLightfv(GL_LIGHT0 + currentLight, GL_DIFFUSE, light);
  299.         light[0] = 1.0;
  300.         light[1] = 1.0;
  301.         light[2] = 1.0;
  302.         light[3] = 1.0;
  303.         glLightfv(GL_LIGHT0 + currentLight, GL_SPECULAR, light);
  304.         light[0] = cos(theta);
  305.         light[1] = sin(theta);
  306.         light[2] = 0.5;
  307.             light[3] = 0.0;
  308.         glLightfv(GL_LIGHT0 + currentLight, GL_POSITION, light);
  309.         theta += two_pi/numLights;
  310.     }
  311.         /* Next, define the local lights */
  312.         for (; currentLight<numLights; currentLight++) {
  313.             CalcRGBColor(theta/two_pi, &light[0], &light[1], &light[2]);
  314.             light[3] = 1.0;
  315.             glLightfv(GL_LIGHT0 + currentLight, GL_DIFFUSE, light);
  316.             light[0] = 1.0;
  317.             light[1] = 1.0;
  318.             light[2] = 1.0;
  319.             light[3] = 1.0;
  320.             glLightfv(GL_LIGHT0 + currentLight, GL_SPECULAR, light);
  321.             light[0] = cos(theta);
  322.             light[1] = sin(theta);
  323.             light[2] = 0.5;
  324.             light[3] = 1.0;
  325.             glLightfv(GL_LIGHT0 + currentLight, GL_POSITION, light);
  326.             theta += two_pi/numLights;
  327.         }
  328.     glEnable(GL_LIGHTING);
  329.  
  330.     /* Load materials */
  331.     glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, this->shininess);
  332.     if (!this->environ.bufConfig.rgba) {
  333.         GLint colorIndexes[3];
  334.         int maxIndex = (1 << this->environ.bufConfig.indexSize) - 1;
  335.         colorIndexes[0] = 0;
  336.         colorIndexes[1] = maxIndex;
  337.         colorIndexes[2] = maxIndex;
  338.         glMaterialiv(GL_FRONT_AND_BACK, GL_COLOR_INDEXES, colorIndexes);
  339.     } else {
  340.         if (this->specComp) {
  341.         GLfloat specular[] = { 1., 1., 1., 1. };
  342.         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
  343.         } else {
  344.         GLfloat specular[] = { 0., 0., 0., 1. };
  345.         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
  346.         }
  347.     }
  348.  
  349.     /* Set local viewer */
  350.     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, this->localViewer);
  351.     }
  352.     for (i=0; i<numLights; i++)
  353.     glEnable(GL_LIGHT0 + i);
  354.     for (i=numLights; i<8; i++)
  355.     glDisable(GL_LIGHT0 + i);
  356.  
  357.     if (this->normalData!=None && this->colorData!=None) {
  358.         glColorMaterial(this->colorMatSide, this->colorMaterial);
  359.     glEnable(GL_COLOR_MATERIAL);
  360.     } else {
  361.     glDisable(GL_COLOR_MATERIAL);
  362.     }
  363.  
  364.     glShadeModel(this->shadeModel);
  365.  
  366. #ifdef GL_EXT_vertex_array
  367.     if (this->vertexArray && strstr(this->environ.glExtensions, "GL_EXT_vertex_array")) {
  368.     if (this->colorData == PerVertex)
  369.         if (this->environ.bufConfig.rgba) {
  370.         glEnable(GL_COLOR_ARRAY_EXT);
  371.         glDisable(GL_INDEX_ARRAY_EXT);
  372.         } else {
  373.         glDisable(GL_COLOR_ARRAY_EXT);
  374.         glEnable(GL_INDEX_ARRAY_EXT);
  375.         }
  376.     if (this->normalData == PerVertex)
  377.         glEnable(GL_NORMAL_ARRAY_EXT);
  378.     else
  379.         glDisable(GL_NORMAL_ARRAY_EXT);
  380.     if (this->textureData == PerVertex && this->environ.bufConfig.rgba)
  381.         glEnable(GL_TEXTURE_COORD_ARRAY_EXT);
  382.     else
  383.         glDisable(GL_TEXTURE_COORD_ARRAY_EXT);
  384.     glEnable(GL_VERTEX_ARRAY_EXT);
  385.     } else {
  386.     glDisable(GL_INDEX_ARRAY_EXT);
  387.     glDisable(GL_COLOR_ARRAY_EXT);
  388.     glDisable(GL_NORMAL_ARRAY_EXT);
  389.     glDisable(GL_TEXTURE_COORD_ARRAY_EXT);
  390.     glDisable(GL_VERTEX_ARRAY_EXT);
  391.     }
  392. #endif
  393.  
  394. #ifdef GL_VERSION_1_1
  395.     if (this->vertexArray11) {
  396.     if (this->colorData == PerVertex)
  397.         if (this->environ.bufConfig.rgba) {
  398.         glEnableClientState(GL_COLOR_ARRAY);
  399.         glDisableClientState(GL_INDEX_ARRAY);
  400.         } else {
  401.         glDisableClientState(GL_COLOR_ARRAY);
  402.         glEnableClientState(GL_INDEX_ARRAY);
  403.         }
  404.     if (this->normalData == PerVertex)
  405.         glEnableClientState(GL_NORMAL_ARRAY);
  406.     else
  407.         glDisableClientState(GL_NORMAL_ARRAY);
  408.     if (this->textureData == PerVertex && this->environ.bufConfig.rgba)
  409.         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  410.     else
  411.         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  412.     glEnableClientState(GL_VERTEX_ARRAY);
  413.     } else {
  414.     glDisableClientState(GL_INDEX_ARRAY);
  415.     glDisableClientState(GL_COLOR_ARRAY);
  416.     glDisableClientState(GL_NORMAL_ARRAY);
  417.     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  418.     glDisableClientState(GL_VERTEX_ARRAY);
  419.     }
  420. #endif
  421.  
  422.     return 0;
  423. }
  424.  
  425. static void AddNormalData(GLfloat* data, const GLfloat x, const GLfloat y)
  426. {
  427.     const GLfloat root2 = sqrt(2.0);
  428.  
  429.     if (x < -1.0 || 1.0 < x || y < -1.0 || 1.0 < y) {
  430.     *data++ = 0.0;
  431.     *data++ = 0.0;
  432.     *data++ = 1.0;
  433.     } else {
  434.     *data++ = x/root2;
  435.     *data++ = y/root2;
  436.     *data++ = sqrt(2.0 - x*x - y*y)/root2;
  437.     }
  438. }
  439.  
  440. void Vertex__AddTraversalData(VertexPtr this)
  441. /*
  442. // This will take numVertices of 3d float coordinates pointed to by traversalData, and
  443. // add the appropriate facet/vertex color, normal, and texture data.
  444. */
  445. {
  446.     GLfloat x, y;
  447.     int i, j, k;
  448.     int facetDataSize, vertexDataSize, dataSize;
  449.     GLfloat* newTraverseData; 
  450.     GLfloat* newptr;
  451.     GLfloat* ptr = this->traversalData;
  452.     int rgba = this->environ.bufConfig.rgba;
  453.     int colorData = this->colorData;
  454.     int normalData = this->normalData;
  455.     int textureData = this->textureData;
  456.     int numBgnEnds = this->numBgnEnds;
  457.     int facetsPerBgnEnd = this->facetsPerBgnEnd;
  458.     int vertsPerFacet = this->vertsPerFacet;
  459.     const GLfloat colorFactor = 0.8;
  460.     GLfloat texFactorX, texFactorY, texFactorZ;
  461.     int windowDim = min(this->environ.windowWidth, this->environ.windowHeight);
  462.     int vertexColorSize, vertexNormalSize, vertexTexSize;
  463.     int rampsize = rgba ? 0 : (1 << this->environ.bufConfig.indexSize);
  464.  
  465.     /* These variables are for ordering depth values */
  466.     GLdouble modelMatrix[16];
  467.     GLdouble projMatrix[16];
  468.     GLint viewport[4];
  469.     GLdouble xd, yd, zd;
  470.     GLdouble depthBits, epsilon;
  471.     GLdouble base, range, delta;
  472.  
  473.     facetDataSize = (colorData == PerFacet) ? ((rgba) ? this->colorDim : 1) : 0;
  474.     facetDataSize += (normalData == PerFacet) ? 3 : 0;
  475.     vertexColorSize = (colorData == PerVertex) ? ((rgba) ? this->colorDim : 1) : 0;
  476.     vertexNormalSize = (normalData == PerVertex) ? 3 : 0;
  477.     switch (this->texture) {
  478.     case Off:
  479.     vertexTexSize = 0;
  480.     break;
  481.     case GL_TEXTURE_1D:
  482.     vertexTexSize = (textureData == PerVertex) ? 1 : 0;
  483.     break;
  484.     case GL_TEXTURE_2D:
  485.     vertexTexSize = (textureData == PerVertex) ? 2 : 0;
  486.     break;
  487. #ifdef GL_EXT_texture3D
  488.     case GL_TEXTURE_3D_EXT:
  489.     vertexTexSize = (textureData == PerVertex) ? 3 : 0;
  490.     break;
  491. #endif
  492. #ifdef GL_SGIS_texture4D
  493.     case GL_TEXTURE_4D_SGIS:
  494.     vertexTexSize = (textureData == PerVertex) ? 4 : 0;
  495.     break;
  496. #endif
  497.     }
  498.     vertexTexSize = rgba ? vertexTexSize : 0;
  499.     vertexDataSize = vertexColorSize + vertexNormalSize + vertexTexSize + this->vertexDim;
  500.     dataSize = numBgnEnds * ( facetsPerBgnEnd * (facetDataSize + vertsPerFacet * vertexDataSize));
  501.     newTraverseData = (GLfloat*)AlignMalloc(dataSize * sizeof(GLfloat), this->memAlignment);
  502.     newptr = newTraverseData;
  503.  
  504. #ifdef GL_EXT_vertex_array
  505.     if (this->vertexArray) {
  506.     this->vertexStride = vertexDataSize * sizeof(GLfloat);
  507.     this->bgnendSize = this->vertsPerBgnEnd * vertexDataSize * sizeof(GLfloat);
  508.     this->colorPtr = (void*)newTraverseData;
  509.     this->indexPtr = (void*)newTraverseData;
  510.     this->normalPtr = (void*)(newTraverseData + vertexColorSize);
  511.     this->texPtr = (void*)(newTraverseData + vertexColorSize + vertexNormalSize);
  512.     this->vertexPtr = (void*)(newTraverseData + vertexColorSize + vertexNormalSize + vertexTexSize);
  513.     }
  514. #endif
  515.  
  516. #ifdef GL_VERSION_1_1
  517.     if (this->vertexArray11) {
  518.     this->vertexStride = vertexDataSize * sizeof(GLfloat);
  519.     this->bgnendSize = this->vertsPerBgnEnd * vertexDataSize * sizeof(GLfloat);
  520.     this->colorPtr = (void*)newTraverseData;
  521.     this->indexPtr = (void*)newTraverseData;
  522.     this->normalPtr = (void*)(newTraverseData + vertexColorSize);
  523.     this->texPtr = (void*)(newTraverseData + vertexColorSize + vertexNormalSize);
  524.     this->vertexPtr = (void*)(newTraverseData + vertexColorSize + vertexNormalSize + vertexTexSize);
  525.     }
  526. #endif
  527.  
  528.     if (this->vertexDim == 3 && this->zOrder != Coplanar) {
  529.     GLdouble numVertices = numBgnEnds * facetsPerBgnEnd * vertsPerFacet;
  530.         glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
  531.         glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
  532.         glGetIntegerv(GL_VIEWPORT, viewport);
  533.         glGetDoublev(GL_DEPTH_BITS, &depthBits);
  534.     epsilon = pow(2.0, -depthBits);
  535.         mysrand(15000);
  536.  
  537.     switch (this->zOrder) {
  538.     case Random:
  539.         range = 1. - epsilon;
  540.         base = epsilon;
  541.         delta = 0.;
  542.         break;
  543.     case BackToFront:
  544.         range = (1. - epsilon) / numVertices;
  545.         base = 1. - range - epsilon;
  546.         delta = -range;
  547.         break;
  548.     case FrontToBack:
  549.         range = (1. - epsilon) / numVertices;
  550.         base = epsilon;
  551.         delta = range;
  552.         break;
  553.     }
  554.     }
  555.  
  556.     /* Figure out texture scaling factors given desired texture LOD */
  557.     if (textureData == PerVertex && this->environ.bufConfig.rgba) {
  558.         texFactorX = pow(2., this->texLOD) * 
  559.                      (float)this->environ.windowWidth / (float)this->texWidth;
  560.         texFactorY = pow(2., this->texLOD) * 
  561.                      (float)this->environ.windowHeight / (float)this->texHeight;
  562. #ifdef GL_EXT_texture3D
  563.         /* This will need to be fixed at some point... */
  564.         texFactorZ = pow(2., this->texLOD);
  565. #endif
  566.     }
  567.  
  568.     for (i=0; i<numBgnEnds; i++) {
  569.     x = *ptr++;
  570.     y = *ptr++;
  571.     for (j=0; j<facetsPerBgnEnd; j++) {
  572.         if (j != 0) {
  573.             x = *ptr++;
  574.             y = *ptr++;
  575.         }
  576.         if (colorData == PerFacet)
  577.         if (rgba) {
  578.             if (this->colorDim == 3) {
  579.             AddColorRGBData(newptr, x, y, colorFactor);
  580.             newptr += 3;
  581.             } else {
  582.             AddColorRGBAData(newptr, x, y, colorFactor);
  583.             newptr += 4;
  584.             }
  585.         } else {
  586. #ifdef GL_SGI_array_formats
  587.             if (this->vertexArray11 && this->interleavedData) {
  588.             AddColorCIDataUI(newptr, x, y, windowDim, rampsize);
  589.             } else
  590. #endif
  591.             AddColorCIData(newptr, x, y, windowDim, rampsize);
  592.             newptr += 1;
  593.         }
  594.         if (normalData == PerFacet) {
  595.         AddNormalData(newptr, x, y);
  596.         newptr += 3;
  597.         }
  598.         for (k=0; k<vertsPerFacet; k++) {
  599.         if (k != 0) {
  600.             x = *ptr++;
  601.             y = *ptr++;
  602.         }
  603.         if (colorData == PerVertex)
  604.             if (rgba) {
  605.             if (this->colorDim == 3) {
  606.                 AddColorRGBData(newptr, x, y, colorFactor);
  607.                 newptr += 3;
  608.             } else {
  609.                 AddColorRGBAData(newptr, x, y, colorFactor);
  610.                 newptr += 4;
  611.             }
  612.             } else {
  613. #ifdef GL_SGI_array_formats
  614.             if (this->vertexArray11 && this->interleavedData) {
  615.                 AddColorCIDataUI(newptr, x, y, windowDim, rampsize);
  616.             } else
  617. #endif
  618.             AddColorCIData(newptr, x, y, windowDim, rampsize);
  619.             newptr += 1;
  620.             }
  621.         if (normalData == PerVertex) {
  622.             AddNormalData(newptr, x, y);
  623.             newptr += 3;
  624.         }
  625.         if (textureData == PerVertex && this->environ.bufConfig.rgba) {
  626.             if (this->texture == GL_TEXTURE_1D) {
  627.                 AddTexture1DData(newptr, x, y, texFactorX);
  628.                 newptr += 1;
  629.             } else if (this->texture == GL_TEXTURE_2D) {
  630.                 AddTexture2DData(newptr, x, y, texFactorX, texFactorY);
  631.                 newptr += 2;
  632. #ifdef GL_EXT_texture3D
  633.             } else if (this->texture == GL_TEXTURE_3D_EXT) {
  634.                 AddTexture3DData(newptr, x, y, texFactorX, texFactorY, texFactorZ);
  635.                 newptr += 3;
  636. #endif
  637.             }
  638.         }
  639.         if (this->vertexDim == 2) {
  640.             *newptr++ = x;
  641.             *newptr++ = y;
  642.         } else { /* vertexDim == 3 */
  643.             if (this->zOrder != Coplanar) {
  644.             GLdouble vertex = k + vertsPerFacet * (j + i * facetsPerBgnEnd);
  645.                     GLdouble z = base + 
  646.                                      delta * vertex +
  647.                                      range * (GLdouble)myrand()/(GLdouble)MY_RAND_MAX;
  648.                 gluUnProject((x+1.)/2.*(GLfloat)windowDim,
  649.                       (y+1.)/2.*(GLfloat)windowDim,
  650.                      z,
  651.                      modelMatrix,
  652.                      projMatrix,
  653.                      viewport,
  654.                      &xd, &yd, &zd);
  655.                 *newptr++ = (GLfloat)xd;
  656.                 *newptr++ = (GLfloat)yd;
  657.                 *newptr++ = (GLfloat)zd;
  658.             } else {
  659.                 *newptr++ = x;
  660.                 *newptr++ = y;
  661.                 *newptr++ = -1.;
  662.             }
  663.         }
  664.         }
  665.     }
  666.     }
  667.     free(this->traversalData);
  668.     this->traversalData = newTraverseData;
  669. }
  670.  
  671. void Vertex__Layout(VertexPtr this)
  672. /*
  673.  * Expected inputs to this routine (elements set in Vertex struct):
  674.  *   layoutPoints
  675.  *   layoutLeft
  676.  *   layoutRight
  677.  *   layoutBottom
  678.  *   layoutTop
  679.  *   layoutPadding
  680.  *   layoutSides
  681.  *   acceptObjs
  682.  *   rejectObjs
  683.  *   clipObjs
  684.  * Expected outputs:
  685.  *   traversalData
  686.  *
  687.  * This will produce layoutPoints 2d float coords with the appropriate number trivially
  688.  * accepted, trivially rejected, and clipped (on the boundary) as determined by the
  689.  * acceptObjs, rejectObjs, and clipObjs variables, respectively.  
  690.  *
  691.  * Trivially accepted points will have coordinates:
  692.  *   layoutLeft + layoutPadding <= x <= layoutRight - layoutPadding
  693.  *   layoutBottom + layoutPadding <= y <= layoutTop - layoutPadding
  694.  *   (the visible region of the screen is from -1.0 to 1.0 in each coordinates,
  695.  *    so set these left,right,top,bottom,padding to appropriate values)
  696.  *
  697.  * Clipped objects are put on the boundary of 1 to 4 sides, as determined by the
  698.  * layoutSides variable.  This enables accurate clipping to non-square windows,
  699.  * when it's best to have (layoutSides==2), and have all the clipped points lie 
  700.  * on the left and bottom edges of the window.  This is because the viewport may 
  701.  * not line up with the right and top sides of the window.
  702.  *
  703.  * This function will malloc the appropriate amount of space and snap the 
  704.  * traversalData variable to it.
  705.  */
  706. {
  707.     int             numacc, numrej, numclip;
  708.     int             i;
  709.     int             sidecount;
  710.     GLfloat         acceptObjs = this->acceptObjs;
  711.     GLfloat         rejectObjs = this->rejectObjs;
  712.     GLfloat         clipObjs = this->clipObjs;
  713.     int             layoutPoints = this->layoutPoints;
  714.     GLfloat*        traversalData;
  715.     GLfloat         xstep, ystep;
  716.     GLfloat         left = this->layoutLeft;
  717.     GLfloat         right = this->layoutRight;
  718.     GLfloat         bottom = this->layoutBottom;
  719.     GLfloat         top = this->layoutTop;
  720.     int             sides = this->layoutSides;
  721.     int             side;
  722.     GLfloat         cx0[4], cx1[4], cy0[4], cy1[4];
  723.     GLfloat         segments;
  724.     GLfloat         p;
  725.     
  726.     if (fabs(1.0 - acceptObjs - rejectObjs - clipObjs) > .01) {
  727.         printf("AcceptObjs + RejectObjs + ClipObjs not equal to 1\n");
  728.         exit(1);
  729.     }
  730.  
  731.     this->traversalData = (GLfloat*)malloc(layoutPoints * sizeof(GLfloat) * 2);
  732.     CheckMalloc(this->traversalData);
  733.     traversalData = this->traversalData;
  734.  
  735.     numacc = (int) (acceptObjs * ((GLfloat) layoutPoints));
  736.     numrej = (int) (rejectObjs * ((GLfloat) layoutPoints));
  737.     numclip = (int) (clipObjs * ((GLfloat) layoutPoints));
  738.  
  739.     numacc += layoutPoints - numacc - numrej - numclip;
  740.  
  741.     /* Set variables and coefficients for finding points on boundary */
  742.     segments = (GLfloat)((numclip + sides - 1)/sides + 1);
  743.     xstep = (right - left)/segments;
  744.     ystep = (top - bottom)/segments;
  745.     cx0[0] = left;
  746.     cx1[0] = xstep;
  747.     cy0[0] = bottom;
  748.     cy1[0] = 0.;
  749.     cx0[1] = left;
  750.     cx1[1] = 0.;
  751.     cy0[1] = top;
  752.     cy1[1] = -ystep;
  753.     cx0[2] = right;
  754.     cx1[2] = -xstep;
  755.     cy0[2] = top;
  756.     cy1[2] = 0.;
  757.     cx0[3] = right;
  758.     cx1[3] = 0.;
  759.     cy0[3] = bottom;
  760.     cy1[3] = ystep;
  761.     
  762.     for (i = 0; i < numclip; i++) {
  763.         p = (GLfloat)((i + sides) / sides);
  764.         side = i % sides;
  765.         *traversalData++ = cx1[side] * p + cx0[side];
  766.         *traversalData++ = cy1[side] * p + cy0[side];
  767.     }
  768.  
  769.     for (i = 0; i < numrej; i++) {
  770.         *traversalData++ = -2.0;
  771.         *traversalData++ = 2.0;
  772.     }
  773.  
  774.     /* Set variables for finding points in the interior */
  775.     left   += this->layoutPadding;
  776.     right  -= this->layoutPadding;
  777.     bottom += this->layoutPadding;
  778.     top    -= this->layoutPadding;
  779.     sidecount = (int)ceil(sqrt((double)numacc));
  780.     segments = (sidecount==1) ? 1. : (GLfloat)(sidecount - 1);
  781.     xstep = (right - left)/segments;
  782.     ystep = (top - bottom)/segments;
  783.  
  784.     for (i = 0; i < numacc; i++) {
  785.     *traversalData++ = left   + (GLfloat)(i % sidecount) * xstep;
  786.     *traversalData++ = bottom + (GLfloat)(i / sidecount) * ystep;
  787.     }
  788. }
  789.